package com.hunlian.makelove.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Handler;
import android.os.Message;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hunlian.makelove.R;
import com.hunlian.makelove.common.AppConfig;
import com.hunlian.makelove.common.DateUtils;
import com.hunlian.makelove.common.StringUtil;
import com.hunlian.makelove.bean.Person;
import com.hunlian.makelove.view.CustomProgressDialog;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ListAdapterReceiveInviting extends ModelAdapter<Person> {

	private Activity mContext;
	private ImageLoader mImageLoader;
	private DisplayImageOptions mOptions_ad;
	private String oppositeUserId;
	private ListAdapterReceiveInviting mAdapter;
	private SparseArray<Integer> mSelectState;
	private int mYear;

	public ListAdapterReceiveInviting(Activity mContext) {
		super();
		this.mContext = mContext;
		mAdapter = this;

		// 设置在loading时的默认图片
		mOptions_ad = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.cacheInMemory(true).cacheOnDisk(true).build();
		mImageLoader = ImageLoader.getInstance();

		if(StringUtil.isEmpty(AppConfig.netTime)){
			String localTime = DateUtils.getLocalLongDate().substring(0,4);
			mYear = Integer.valueOf(localTime);
		}else{
			String localTime = AppConfig.netTime.substring(0,4);
			mYear = Integer.valueOf(localTime);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final MyViewHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.receive_inviting_item, null);
			holder = new MyViewHolder();

			holder.mIvHead = (ImageView) convertView.findViewById(R.id.iv_head);
			holder.mTvId = (TextView) convertView.findViewById(R.id.tv_id);
			holder.mTvNickName = (TextView) convertView
					.findViewById(R.id.tv_nickname);
			holder.mTvAddress = (TextView) convertView
					.findViewById(R.id.tv_address);
			holder.mTvEducation = (TextView) convertView
					.findViewById(R.id.tv_education);
			holder.mTvBirthday = (TextView) convertView
					.findViewById(R.id.tv_birthday);
			holder.mRuqest = (TextView) convertView
					.findViewById(R.id.tv_request);
			holder.mIntro = (TextView) convertView.findViewById(R.id.tv_intro);
			holder.mStatus = (TextView) convertView.findViewById(R.id.tv_status);
			
			convertView.setTag(holder);
		} else {
			holder = (MyViewHolder) convertView.getTag();
		}
		

		final Person bean;

		bean = getModel(position);

		mImageLoader
				.displayImage(bean.getPhoto(), holder.mIvHead, mOptions_ad);
		
		
		oppositeUserId = bean.getUserId();
		

		holder.mTvId.setText("ID :"+StringUtil.handleNull(oppositeUserId));
		holder.mTvNickName.setText("昵称 :"+StringUtil.handleNull(bean.getNickname()));
		holder.mTvAddress.setText(StringUtil.handleNull(bean.getAddress()));
		String sEdu = StringUtil.handleNull(bean.getEducation());
		if(sEdu.length()==0){
			holder.mTvEducation.setText("");
		}else{
			holder.mTvEducation.setText(" | "+sEdu);
		}
		String syear = StringUtil.handleNull(bean.getBirthday());
		if(syear.length()==0){
			holder.mTvBirthday.setText("");
		}else{
			int birYear = Integer.valueOf(syear.substring(0, 4));
			holder.mTvBirthday.setText(" | "+(mYear-birYear)+"岁");
		}
		holder.mRuqest.setText("期望 :"+StringUtil.handleNull(bean.getExpect()));
		holder.mIntro.setText("补充介绍 :"+StringUtil.handleNull(bean.getIntroduce()));
		String status = StringUtil.handleNull(bean.getStatus());
		if("0".equals(status)){
			holder.mStatus.setText("去处理");
			holder.mStatus.setTextColor(mContext.getResources().getColor(R.color.white));
			holder.mStatus.setBackgroundResource(R.drawable.oval_pink_bg);

			//给椭圆形的文字背景添加边距
			int left = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10,
					mContext.getResources().getDisplayMetrics());
			int top = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,3,
					mContext.getResources().getDisplayMetrics());
			int right = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10,
					mContext.getResources().getDisplayMetrics());
			int bottom = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,3,
					mContext.getResources().getDisplayMetrics());
			holder.mStatus.setPadding(left,top,right,bottom);


		}else if("1".equals(status)){
			holder.mStatus.setText("已同意");
			holder.mStatus.setTextColor(mContext.getResources().getColor(R.color.base_color));
			holder.mStatus.setBackgroundResource(0);
		}else if("2".equals(status)){
			holder.mStatus.setText("已拒绝");
			holder.mStatus.setTextColor(mContext.getResources().getColor(R.color.base_color));
			holder.mStatus.setBackgroundResource(0);
		}


		return convertView;
	}

	class MyViewHolder {
		private ImageView mIvHead;
		private TextView mTvId;
		private TextView mTvNickName;
		private TextView mTvAddress;
		private TextView mTvEducation;
		private TextView mTvBirthday;
		private TextView mRuqest;
		private TextView mIntro;
		private TextView mStatus;
	}

	public void setSelectedArray(SparseArray<Integer> selectState){
		mSelectState = selectState;
	}
	
	
	protected CustomProgressDialog progressDialog = null;
	
	protected void createProgressDialog(Context context){
		progressDialog = CustomProgressDialog.createDialog(context, mContext.getResources().getString(R.string.loading));
		progressDialog.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
				//若是网络请求，则取消请求——但不能全部取消，防止多线程异步请求被取消
				
			}
		});
	}

	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			
			super.handleMessage(msg);
		}
		
	};
}
